package com.xiaoer.studyenglishdemo.util;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xiaoer.studyenglishdemo.bean.WordBean;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class WordsDBDao {
    public static WordBean insert(Context context, String imgUrl, String introduce, String means, String shortSentence, String sentence1, String sentence2, String sentence3, WordBean.WORD_LEVEL wordLevel, WordBean.STUDY_TYPE st, int localimg) {
        return insert(context, getNextId(context), imgUrl, introduce, means, shortSentence, sentence1, sentence2, sentence3, wordLevel, st, localimg);
    }

    public static WordBean insert(Context context, int id, String imgUrl, String introduce, String means, String shortSentence, String sentence1, String sentence2, String sentence3, WordBean.WORD_LEVEL wordLevel, WordBean.STUDY_TYPE st, int localimg) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("id", id);
        values.put("imgurl", imgUrl);
        values.put("introduce", introduce);
        values.put("means", means);
        values.put("shortsentence", shortSentence);
        values.put("sentence1", sentence1);
        values.put("sentence2", sentence2);
        values.put("sentence3", sentence3);
        values.put("wordlevel", wordLevel.toString());
        values.put("studytype", st.toString());
        values.put("localimg", localimg);
        long insert = db.insert(WordsDBHelper.TABLE_WORDS, null, values);
        if (insert > 0) {
            db.close();
            sqLiteOpenHelper.close();
            return new WordBean(id, imgUrl, introduce, means, shortSentence, sentence1, sentence2, sentence3, wordLevel, st, localimg);
        }
        db.close();
        sqLiteOpenHelper.close();
        return null;
    }

    public static boolean delete(Context context, int id) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        int del = db.delete(WordsDBHelper.TABLE_WORDS, "id=?", new String[]{String.valueOf(id)});
        if (del > 0) {
            db.close();
            sqLiteOpenHelper.close();
            return true;
        }
        db.close();
        sqLiteOpenHelper.close();
        return false;
    }

    public static List<WordBean> selectAll(Context context) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + WordsDBHelper.TABLE_WORDS, null);
        return handleSelResult(sqLiteOpenHelper, db, cursor);
    }

    public static List<WordBean> selectById(Context context, int _id) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + WordsDBHelper.TABLE_WORDS + " where id = ?", new String[]{String.valueOf(_id)});
        return handleSelResult(sqLiteOpenHelper, db, cursor);
    }

    public static List<WordBean> selectByWordLevel(Context context, WordBean.WORD_LEVEL wl) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + WordsDBHelper.TABLE_WORDS + " where wordlevel = ?", new String[]{wl.toString()});
        return handleSelResult(sqLiteOpenHelper, db, cursor);
    }

    public static List<WordBean> selectByWordLevelAndStudyType(Context context, WordBean.WORD_LEVEL wl, WordBean.STUDY_TYPE st) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + WordsDBHelper.TABLE_WORDS + " where wordlevel = ? and studytype = ?", new String[]{wl.toString(), st.toString()});
        return handleSelResult(sqLiteOpenHelper, db, cursor);
    }

    public static List<WordBean> selectByStudyType(Context context, WordBean.STUDY_TYPE st) {
        SQLiteOpenHelper sqLiteOpenHelper = new WordsDBHelper(context);
        SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from " + WordsDBHelper.TABLE_WORDS + " where studytype = ?", new String[]{st.toString()});
        return handleSelResult(sqLiteOpenHelper, db, cursor);
    }

    @SuppressLint("Range")
    private static List<WordBean> handleSelResult(SQLiteOpenHelper sqLiteOpenHelper, SQLiteDatabase db, Cursor cursor) {
        List<WordBean> beans = new ArrayList<>();
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String imgUrl = cursor.getString(cursor.getColumnIndex("imgurl"));
                String introduce = cursor.getString(cursor.getColumnIndex("introduce"));
                String means = cursor.getString(cursor.getColumnIndex("means"));
                String shortSentence = cursor.getString(cursor.getColumnIndex("shortsentence"));
                String sentence1 = cursor.getString(cursor.getColumnIndex("sentence1"));
                String sentence2 = cursor.getString(cursor.getColumnIndex("sentence2"));
                String sentence3 = cursor.getString(cursor.getColumnIndex("sentence3"));
                String wordLevel = cursor.getString(cursor.getColumnIndex("wordlevel"));
                String studyType = cursor.getString(cursor.getColumnIndex("studytype"));
                int imgLocal = cursor.getInt(cursor.getColumnIndex("localimg"));
                beans.add(new WordBean(id, imgUrl, introduce, means, shortSentence, sentence1, sentence2, sentence3, WordBean.WORD_LEVEL.valueOf(wordLevel), WordBean.STUDY_TYPE.valueOf(studyType), imgLocal));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        sqLiteOpenHelper.close();
        return beans;
    }

    public static int getNextId(Context context) {
        List<WordBean> beans = selectAll(context);
        if (beans == null || beans.size() == 0) return 1;
        return beans.size() + 1;
    }
}
